package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionExpertProgressService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.controller.vo.BeSectionStepVO;
import com.bidevalution.dao.entity.BeSectionExpertProgress;
import com.bidevalution.dao.entity.BeSectionStep;
import com.qtp.core.model.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by daizhifeng on 2019-08-19.
 */
@Log4j2
@RestController
@RequestMapping("/beSectionStep")
@Api(tags = "标段环节")
public class BeSectionStepController extends BaseController {
    @Autowired
    private BeSectionStepService beSectionStepService;
    @Autowired
    private BeSectionExpertProgressService beSectionExpertProgressService;

    @GetMapping("/getSectionStepList")
    @ApiOperation(value = "标段环节-根据标段id获取标段环节列表", response = BeSectionStep.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
            })
    public Response<List<BeSectionStepVO>> getBeSectionStepList(@RequestParam("sectionId") String sectionId) {
        try {
        	List<BeSectionStepVO> sectionStepVOs = new ArrayList<BeSectionStepVO>();
        	BeSectionStepVO beSectionStepVO;
        	for(BeSectionStep beSectionStep:beSectionStepService.getChildSectionStepList(sectionId,"0")) {
        		beSectionStepVO = new BeSectionStepVO();
        		beSectionStepVO.setLoadFunction(beSectionStep.getLoadFunction());
        		beSectionStepVO.setOrderNum(beSectionStep.getOrderNum());
        		beSectionStepVO.setSectionId(sectionId);
        		beSectionStepVO.setSeqId(beSectionStep.getSeqId());
        		beSectionStepVO.setStepCode(beSectionStep.getStepCode());
        		beSectionStepVO.setStepName(beSectionStep.getStepName());
        		beSectionStepVO.setStepUrl(beSectionStep.getStepUrl());
        		beSectionStepVO.setIsFinish(beSectionExpertProgressService.getFinishStatus(beSectionStep.getSeqId()));
        		sectionStepVOs.add(beSectionStepVO);
        	}
            return Response.ok(sectionStepVOs);
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }
//通过父节点查询子节点没有功能使用
//    @GetMapping("/getChildSectionStepList")
//    @ApiOperation(value = "标段环节-根据当前环节id获取子环节列表", response = BeSectionStep.class)
//    @ApiImplicitParams(
//            {
//                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "parentId", value = "环节主键", required = true),
//            })
//    public Response<List<BeSectionStep>> getChildSectionStepList(@RequestParam("parentId") String parentId) {
//        try {
//            return Response.ok(beSectionStepService.getChildSectionStepList(parentId));
//        } catch (Exception e) {
//            return Response.fail(e.getMessage());
//        }
//    }

    @GetMapping("/getSectionStepBySeqId")
    @ApiOperation(value = "标段环节-根据当前环节id查询环节信息", response = BeSectionStep.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "seqId", value = "环节主键", required = true)
            })
    public Response<BeSectionStep> getSectionStepBySeqId(@RequestParam("seqId") String seqId) {
        try {
            return Response.ok(beSectionStepService.getSectionStepBySeqId(seqId));
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }

    @GetMapping("/getBeSectionStepReappraisalList")
    @ApiOperation(value = "标段环节-重评时根据标段id获取标段环节列表", response = BeSectionStep.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段id", required = true),
            })
    public Response<List<Map<String, Object>>> getBeSectionStepReappraisalList(@RequestParam("sectionId") String sectionId) {
        if(StringUtils.isBlank(sectionId)){
            return Response.fail("参数不可以为空！");
        }
        try {
            return Response.ok(beSectionStepService.getBeSectionStepReappraisalList(sectionId));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Response.fail("系统异常，请联系管理员！");
        }
    }

    @GetMapping("/getSectionStepByCode")
    @ApiOperation(value = "标段环节-根据当前环节代码和项目ID查询环节信息", response = BeSectionStep.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "项目ID", required = true),
                    @ApiImplicitParam(paramType = "query", dataType = "String", name = "stepCode", value = "环节代码", required = true)
            })
    public Response<BeSectionStepVO> getSectionStepByCode(@RequestParam("sectionId")String sectionId ,@RequestParam("stepCode") String stepCode) {
        try {
        	BeSectionStepVO beSectionStepVO = new BeSectionStepVO();
        	BeSectionStep beSectionStep = beSectionStepService.getSectionStepByCode(sectionId, stepCode);
        	beSectionStepVO.setLoadFunction(beSectionStep.getLoadFunction());
        	beSectionStepVO.setStepUrl(beSectionStep.getStepUrl());
        	beSectionStepVO.setOrderNum(beSectionStep.getOrderNum());
        	beSectionStepVO.setSectionId(beSectionStep.getSectionId());
        	beSectionStepVO.setSeqId(beSectionStep.getSeqId());
        	beSectionStepVO.setStepName(beSectionStep.getStepName());
        	beSectionStepVO.setStepCode(beSectionStep.getStepCode());
        	beSectionStepVO.setIsFinish(beSectionExpertProgressService.getFinishStatus(beSectionStep.getSeqId()));
            return Response.ok(beSectionStepVO);
        } catch (Exception e) {
            return Response.fail(e.getMessage());
        }
    }
}
